package com.loong.user.controller.backend;

import com.loong.common.domain.BaseQuery;
import com.loong.common.enumerate.ResultCode;
import com.loong.common.exception.GlobalException;
import com.loong.common.mvc.BaseBackendController;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonUpdate;
import com.loong.common.validator.group.CommonValidate;
import com.loong.user.common.backend.param.ManageApiParam;
import com.loong.user.common.backend.param.ManageApiQueryParam;
import com.loong.user.handler.ManageApiHandler;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description: 后台api管理
 * @Author: hzp
 * @Date: 2020-10-16 10:25
 */
@RestController
@RequestMapping("/manage/user/systemApi")
public class ManageApiController extends BaseBackendController {
    @Resource
    private ManageApiHandler manageApiHandler;

    /**
     * 保存系统后台api管理
     * @param param
     * @return
     */
    @PostMapping("save")
    public Result save(@Validated(value = {CommonInsert.class, CommonValidate.class}) ManageApiParam param){
        param.setId(null);
        return   handlerExecutor.execute(()->manageApiHandler.save(param));
    }

    /**
     * 更新系统后台api管理
     * @param param
     * @return
     */
    @PostMapping("update")
    public Result update(@Validated(value = {CommonUpdate.class, CommonValidate.class}) ManageApiParam param){
        if(ObjectUtils.isEmpty(param)){
            throw new GlobalException(ResultCode.PARAM_ERROR);
        }
        return  handlerExecutor.execute(()->manageApiHandler.update(param));
    }

    /**
     *根据id删除系统后台api管理
     * @param id
     */
    @PostMapping("delete")
    public Result deleteById(@RequestParam("id") Long id){
        return handlerExecutor.execute(()->manageApiHandler.deleteById(id));
    }

    /**
     * 分页查询
     * @param baseQuery
     * @param param
     * @return
     */
    @PostMapping("pageQuery")
    public Result pageQuery(BaseQuery baseQuery, ManageApiQueryParam param){
        return  handlerExecutor.execute(()->manageApiHandler.pageQuery(baseQuery,param));
    }

}
